<?php

/**************************************************
 * Class Sms_eee
 * 公司  上海千橙科技有限公司
 * http://sms.106908.com/main  beizhu 密码 rzio1RUo
 */
class Sms_qc
{
    private $userName = 'beizhu';    //用户账号
    private $password = 'rzio1RUo';
    private $signature = '【贝竹】';
    private $http = 'http://112.124.17.46:7001/send?';
    private $error;

    function sendSMS($tel,$content,$date=''){
        $content = $content.$this->signature;
        $content=@mb_convert_encoding($content,'UTF-8','');
        $data = array(
            'username' => $this->userName,
            'password' => $this->password,
            'content'  => $content,
            'mobile'   => $tel,
            'dstime'   => $date				//号码
        );
        $info = $this->postSMS($this->http, $data);

        if($info != 1){
            var_dump($info);
            $this->error = $this->getErrorMessage($info);
            return false;
        }
        return true;
    }

    /**
     * POST提交短信数据
     */
    function postSMS($url,$data=''){
        $row = parse_url($url);
        $host = $row['host'];
        $port = !empty($row['port']) ? $row['port']:80;
        $file = $row['path'];
        $post="";
        while (list($k,$v) = each($data)){
            $post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
        }
        $post = substr($post , 0 , -1 );
        $len = strlen($post);
        $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
        if (!$fp) {
            return "$errstr ($errno)\n";
        } else {
            $receive = '';
            $out = "POST $file HTTP/1.1\r\n";
            $out .= "Host: $host\r\n";
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Content-Length: $len\r\n\r\n";
            $out .= $post;
            fwrite($fp, $out);
            while (!feof($fp)) {
                $receive .= fgets($fp, 128);
            }
            fclose($fp);
            $receive = explode("\r\n\r\n",$receive);
            unset($receive[0]);
            return implode("",$receive);
        }
    }

    private function getErrorMessage($key){
        $data = array(
            -1=>'用户名或者密码不正确',
            0=>'发送短信失败',
            1=>'代表发送短信成功, 批次号用来获取状态',
            2=>'余额不足',
            3=>'扣费失败',
            6=>'有效号码为空',
            7=>'短信内容为空',
            8=>'签名错误。必须带有【输入您的公司简称或产品名称，字数不能超过8个字符】',
            100=>'短信内容有敏感词等，审核失败，实时退费( 客服驳回处理)'
        );
        if(isset($data[$key])){
            return $data[$key];
        }
        return $key;
    }

    public function getError()
    {
        return $this->error;
    }
}